package com.bingwang.sourcelearn.aop.aspects;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @author WangBing
 * @date 2023/11/18 08:41
 */
@Aspect
@Component
public class LogAspect {
    @Pointcut("execution(* com.bingwang.sourcelearn.aop.service.*.*(..))")
    public void pointcut() {}

    @Before("pointcut()")
    public void before() {
        System.out.println("前置通知 before...");
    }

    @After("pointcut()")
    public void after() {
        System.out.println("后置通知 after...");
    }

    @Around("pointcut()")
    public Object around(ProceedingJoinPoint proceedingJoinPoint) {
        try {
            MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();

            System.out.println("method: " + signature.getMethod().getName());
            System.out.println("环绕通知 start...");

            Object result = proceedingJoinPoint.proceed();
            System.out.println("环绕通知 end...");

            return result;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;
    }
}
